#include <freertos/message_buffer.h>
MessageBufferHandle_t messageBuffer = nullptr;

void writeBufferTask(void*)
{
  char data[8] = {0};
  static uint16_t count = 995;
  while(1)
  {
    sprintf(data, "%d", count++);
    uint8_t dataLen = 0;
    for(int i = 0; i < 8; i++)
    {
      if(data[i] != 0)
      {
        dataLen++;
      }      
    }
    xMessageBufferSend(messageBuffer, (void*)data, dataLen, 1000);
    vTaskDelay(1000);
  }
}

void readBufferTask(void*)
{
  char data[64] = {0};
  uint8_t revLength = 0;
  while(1)
  {
    revLength = xMessageBufferNextLengthBytes(messageBuffer);
    Serial.printf("Next buffer Length is %d\n", revLength);
    if(revLength > 0)
    {
      revLength = xMessageBufferReceive(messageBuffer, (void*)data, sizeof(data), 1000);
      Serial.printf("Length: %d, data: %s\n", revLength, data);
    }
    else
    {
      vTaskDelay(200);
    }
  }
}

void setup()
{
  Serial.begin(115200);
  messageBuffer = xMessageBufferCreate(128);
  if(messageBuffer == nullptr)
  {
    Serial.println("xMessageBufferCreate failed.");
    ESP.restart();
  }
  xTaskCreate(writeBufferTask, "writeBufferTask", 2048, nullptr, 1, nullptr);
  xTaskCreate(readBufferTask, "readBufferTask", 2048, nullptr, 1, nullptr);
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}
